using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Pages_Employee_ManageVPPPayment_ViewVPP : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session["VPPSelectMethod"] = "GetAllVPPPayments";
        }
        if (Session["VPPSelectMethod"] != null)
        {
            VPPObjectDataSource.SelectMethod = (string)Session["VPPSelectMethod"];
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        txtSearch.Text = txtSearch.Text.Trim();
        if (txtSearch.Text == "") return;
        if (!Page.IsValid) return;
        VPPObjectDataSource.SelectParameters.Clear();
        if (rdDeliverableID.Checked) //Search by DeliverableID
        {
            Session["VPPSelectMethod"] = "SearchVPPPaymentByDeliverableID";
            VPPObjectDataSource.SelectMethod = (string)Session["VPPSelectMethod"];
            VPPObjectDataSource.SelectParameters.Add("DeliverableID", txtSearch.Text);
        }
        else if (rdCustomerName.Checked) //Search by FullName of customer
        {
            Session["VPPSelectMethod"] = "SearchVPPPaymentByFullName";
            VPPObjectDataSource.SelectMethod = (string)Session["VPPSelectMethod"];
            VPPObjectDataSource.SelectParameters.Add("FullName", txtSearch.Text);
        }
        gvVPPPayments.PageIndex = 0;
        gvVPPPayments.DataBind();
    }

    protected void gvVPPPayments_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvVPPPayments.PageIndex = e.NewPageIndex;
        gvVPPPayments.DataBind();
    }

    protected void custSearch_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (rdDeliverableID.Checked)
        {
            try
            {
                int n = Int32.Parse(args.Value);
                args.IsValid = (n > 0);
            }
            catch
            {
                args.IsValid = false;
            }
        }
    }

    protected void btnAddVPPPayment_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Pages_Employee/ManageVPPPayment/AddVPPPayment.aspx");
    }

    protected void btnDeleteVPPPayment_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        List<string> lstDeliverableIDs = new List<string>();
        // get the selected items
        foreach (GridViewRow row in gvVPPPayments.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cboDelete = (CheckBox)row.Cells[0].FindControl("cboDelete");
                if (cboDelete != null)
                {
                    if (cboDelete.Checked)
                    {
                        string id = row.Cells[0].Text;
                        lstDeliverableIDs.Add(id);
                    }
                }
            }
        }

        // process the deleting
        if (lstDeliverableIDs.Count == 0)
        {
            lblMessage.Text = "Please select some items to delete";
        }
        else
        {
            try
            {
                int result = DALVPPPayment.DeleteVPPPayments(lstDeliverableIDs.ToArray());
                lblMessage.Text = "Deleted successfully";
                gvVPPPayments.DataBind();
            }
            catch
            {
                lblMessage.Text = "Error deleting VPP Payments";
            }
        }
    }
}
